package com.heima.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {


    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {
        //判断客户端传来的参数是否为空
        if (channelDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //检查是否有分页参数
        channelDto.checkParam();

        Page page = new Page();
        page.setPages(channelDto.getPage());
        page.setSize(channelDto.getSize());
        LambdaQueryWrapper<AdChannel> lambdaQuery = Wrappers.lambdaQuery();
        if (StrUtil.isNotEmpty(channelDto.getName())){
            lambdaQuery.like(AdChannel::getName,channelDto.getName());
        }
        //获取查询信息
        IPage iPage = page(page, lambdaQuery);

        //对分页的数据进行封装 :getTotal :当前满足条件总行数
        PageResponseResult responseResult = new PageResponseResult(channelDto.getPage(), channelDto.getSize(), (int) iPage.getTotal());

        //将查询到的数据封装
        responseResult.setData(iPage.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insertChannel(AdChannel adChannel) {
        if (ObjectUtil.isEmpty(adChannel)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //给时间字段添加信息
        adChannel.setCreatedTime(new Date());

        boolean save = save(adChannel);
        if (save){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.ERROR);

    }

    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {
        if (adChannel.getId() == null || adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        boolean update = updateById(adChannel);

        if (update){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdChannel adChannel = getById(id);

        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效,不能删除");
        }

        boolean remove = removeById(id);
        if (remove){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.ERROR);
    }
}
